package com.bzyd.shiro.controller;


import com.bzyd.shiro.common.SysResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Xianyue Dang
 * @since 2021-09-10
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public SysResult login(String username, String password, boolean rememberMe){
        if (StringUtils.isBlank(username)){
            return SysResult.fail("用户名不能为空！");
        }

        if (StringUtils.isBlank(password)){
            return SysResult.fail("密码不能为空！");
        }

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        token.setRememberMe(rememberMe);
        subject.login(token);
        return SysResult.ok();
    }

    @RequestMapping("/logout")
    public SysResult logout(){
        SecurityUtils.getSubject().logout();
        return SysResult.ok("用户登出！");
    }

}

